package com.bawei.day0129.view.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bawei.day0129.Bean.JkzxBean;
import com.bawei.day0129.Bean.JkzxtabBean;
import com.bawei.day0129.Bean.XbBean;
import com.bawei.day0129.Bean.ZsBean;
import com.bawei.day0129.R;
import com.bawei.day0129.base.BaseFragment;
import com.bawei.day0129.databinding.ZdylayoutBinding;
import com.bawei.day0129.utils.adapter.RecAdapter;
import com.bawei.day0129.view.activity.DoctorActivity;
import com.bawei.day0129.view.activity.HealthTestActivity;
import com.bawei.day0129.view.activity.LoginActivity;
import com.bawei.day0129.viewmodel.MainViewModel;
import com.bumptech.glide.Glide;
import com.google.android.material.tabs.TabLayout;
import com.stx.xhb.androidx.XBanner;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

public class OnesFragment extends BaseFragment<MainViewModel, ZdylayoutBinding> {
    private ArrayList<Fragment> list=new ArrayList<>();
    @Override
    protected int initLayout() {
        return R.layout.zdylayout;
    }

    @Override
    protected void initView() {
        dataBinding.loginapp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), LoginActivity.class));
            }
        });
        viewModel.getLiveData().observe(getActivity(), new Observer<XbBean>() {
            @Override
            public void onChanged(XbBean xbBean) {
                dataBinding.xbn.setData(xbBean.getResult(),null);
                dataBinding.xbn.setmAdapter(new XBanner.XBannerAdapter() {
                    @Override
                    public void loadBanner(XBanner banner, Object model, View view, int position) {
                        Glide.with(getActivity())
                                .load(xbBean.getResult().get(position).getImageUrl())
                                .into((ImageView) view);
                    }
                });
            }
        });
        viewModel.getLiveData2().observe(getActivity(), new Observer<ZsBean>() {
            @Override
            public void onChanged(ZsBean zsBean) {
                dataBinding.rec.setLayoutManager(new GridLayoutManager(getContext(),4));
                dataBinding.rec.setAdapter(new CommonAdapter<ZsBean.ResultBean>(getContext(),R.layout.zs_item,zsBean.getResult()) {
                    @Override
                    protected void convert(ViewHolder holder, ZsBean.ResultBean resultBean, int position) {
                        holder.setText(R.id.zs_item_name,resultBean.getDepartmentName());
                        Glide.with(getActivity())
                                .load(resultBean.getPic())
                                .into((ImageView) holder.getView(R.id.zs_item_img));
                        holder.getView(R.id.zs_item_img).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                startActivity(new Intent(getActivity(), DoctorActivity.class));
                            }
                        });
                    }
                });
                dataBinding.zdyCp.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        startActivity(new Intent(getActivity(), HealthTestActivity.class));
                    }
                });
            }
        });
        viewModel.getLiveData3().observe(getActivity(), new Observer<JkzxtabBean>() {
            @Override
            public void onChanged(JkzxtabBean jkzxtabBean) {

                List<JkzxtabBean.ResultBean> result = jkzxtabBean.getResult();
                dataBinding.listTab.removeAllTabs();
                for (JkzxtabBean.ResultBean resultBean : result) {
                    dataBinding.listTab.addTab(dataBinding.listTab.newTab().setText(resultBean.getName()));
                }
                dataBinding.listTab.addOnTabSelectedListener(new TabLayout.BaseOnTabSelectedListener() {
                    @Override
                    public void onTabSelected(TabLayout.Tab tab) {
                        viewModel.getLiveData4(result.get(tab.getPosition()).getId(),1,5);
                    }

                    @Override
                    public void onTabUnselected(TabLayout.Tab tab) {

                    }

                    @Override
                    public void onTabReselected(TabLayout.Tab tab) {

                    }
                });
                viewModel.getLiveData4(1,1,5).observe(getActivity(), new Observer<JkzxBean>() {
                    @Override
                    public void onChanged(JkzxBean jkzxBean) {
                        List<JkzxBean.ResultBean> result1 = jkzxBean.getResult();
                        dataBinding.listRec.setLayoutManager(new LinearLayoutManager(getActivity()));
                        RecAdapter Adapter = new RecAdapter((ArrayList<JkzxBean.ResultBean>) result1, getActivity());
                        dataBinding.listRec.setAdapter(Adapter);
                        dataBinding.listRec.getAdapter().notifyDataSetChanged();
                    }
                });
            }
        });
    }

}
